#include "stdafx.h"

#include "CppUnitTest.h"

#include "ImageProcessFile.h"
#include "ImageProcSteps.h"
#include "TestFiles.h"
#include "ImageExpt.h"
#include "DummyOne.h"

#include "IImageProcStepLibDyn.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace PluginICProcStepsTest
{
	TEST_CLASS(IEFTest)
	{
	private:
		ImageExpt *_ief;
		ImageExpt *_iefTemplate;
		ImageProcStepLibAuto *_Lib;

	public:
		IEFTest::IEFTest()
		{
		}

		IEFTest::~IEFTest()
		{
		}

		TEST_METHOD_INITIALIZE(InitIEFTest)
		{
			if ((_iefTemplate = ImageExpt::CreateImageExpt(TestFiles::UnitTestIEFTemplatewStr, '\0', L"unit test", L"default", ImageExpt::FromDefaults)) != '\0')
			{
				TiXmlElement * contentHdr = new TiXmlElement( "TestMeHeader" );
				_iefTemplate->GetHeader()->LinkEndChild( contentHdr );
				TiXmlElement * contentBody = new TiXmlElement( "TestMeBody" );
				_iefTemplate->GetBody()->LinkEndChild( contentBody );
				_iefTemplate->SaveFile();

				_Lib = new ImageProcStepLibAuto();
				_Lib->SetLibNameVersion("ImageProcStepMxImageLib", "v1.0.0.1", 3);
				_Lib->SetLibOwnerDetails("Maximodex Limited", "Copyright 2012, Will Stott", "www.maximodex.com/ProductSupport/ImageCurator/ImageProcLibs/ImageProcSample");
				_Lib->SetLibDescription("dummy - use only for testing - it doesn't do anything");
			}

			if ((_ief = ImageExpt::CreateImageExpt(TestFiles::UnitTestIEFwStr, TestFiles::UnitTestIEFTemplatewStr, L"unit test", L"default", ImageExpt::FromTempate)) != '\0')
				_ief->SaveFile();
		}

		TEST_METHOD_CLEANUP(DeinitIEFTest)
		{
			if (_iefTemplate != '\0')
			{
				delete _iefTemplate;	//_iefTemplate->DeleteMe();
				_iefTemplate = '\0';
			}
			if (_ief != '\0')
			{
				delete _ief;			//_ief->DeleteMe();
				_ief = '\0';
			}
			if (_Lib != '\0')
			{
				delete _Lib;
				_Lib = '\0';
			}
		}


		TEST_METHOD(CreateIEFTest)
		{
			std::wstring errMsg(L"no error");
			Assert::IsNotNull(TestFiles::Instance().GetPluginLibs(&errMsg), errMsg.c_str() );
			Assert::IsNotNull(_iefTemplate);
			Assert::IsNotNull(_ief);
			Assert::IsNotNull(_Lib);
			Assert::AreEqual( ImageProcStepLibAuto::NotFound, _Lib->GetFunctionCount());
			Assert::IsFalse( _Lib->Validate());		//cannot validate an empty lib
			Assert::IsFalse( _Lib->IsValid());
		}

		TEST_METHOD(IsValidHeaderTest)
		{
			Assert::IsTrue(  _iefTemplate->IsValidHeader());
			Assert::IsTrue( _ief->IsValidHeader());
		}

		TEST_METHOD(IsValidBodyTest)
		{
			Assert::IsTrue( _iefTemplate->IsValidBody());
			Assert::IsTrue( _ief->IsValidBody());
		}

		TEST_METHOD(IsValidIEFTest)
		{
			std::wstring errMsg(L"no error");
			MxPluginLibs *plugins = '\0';
			Assert::IsNotNull(plugins = TestFiles::Instance().GetPluginLibs(&errMsg), errMsg.c_str());

			MxPluginLib *pluginXml = '\0';
			Assert::IsNotNull(pluginXml = plugins->GetPlugin("DEE06017-72D8-45DF-AB37-5012019751D5"));	//this is not IPluginXmlLibIID::GUID() it is same as lib.GetLibID() - see PluginXml, VS_VERSION_INFO, Internal name 
			IPluginXmlDoc *doc = '\0';
			Assert::IsNotNull(doc =  static_cast<IPluginXmlDoc *>(pluginXml->CreateClassInstance(IPluginXmlDocIID::GUID())));

			Assert::IsTrue(doc->Open(TestFiles::UnitTestIEFTemplateStr));
			Assert::IsTrue(doc->Close());

			Assert::IsTrue(doc->Open(TestFiles::UnitTestIEFStr));
			Assert::IsTrue(doc->Close());

			Assert::AreEqual(0L, doc->DestroyPtr());
		}

		TEST_METHOD(GetPathFileNameTest)
		{
			Assert::AreEqual (TestFiles::UnitTestIEFStr, _ief->GetPathFileName() );
		}

		TEST_METHOD(IEFFromFileTest)
		{
			ImageExpt *ief = ImageExpt::CreateImageExpt(TestFiles::UnitTestIEFwStr, '\0', L"unit test", L"default", ImageExpt::FromFile);

			Assert::IsNotNull(ief);
			Assert::IsTrue( ief->SaveFile());

			Assert::IsNotNull(ief->GetHeader() );
			Assert::IsNotNull(ief->GetBody());
			Assert::IsNotNull(ief->GetHeaderProcSteps());
			Assert::IsNotNull(ief->GetBodyImages());

			Assert::IsNotNull(TinyXmlHelper::FindChildXmlElement(ief->GetHeader(), "TestMeHeader"));
			Assert::IsNotNull(TinyXmlHelper::FindChildXmlElement(ief->GetHeader(),  ImageProcSteps::xmlProcSteps));
			Assert::IsNotNull(TinyXmlHelper::FindChildXmlElement(ief->GetBody(), "TestMeBody"));
			Assert::IsNotNull(TinyXmlHelper::FindChildXmlElement(ief->GetBody(), ImageProcessFile::xmlImages));
			Assert::AreEqual(ImageProcessFile::xmlImages, ief->GetBodyImages()->Value());

			delete ief;		//ief->DeleteMe();
		}

		TEST_METHOD(IEFFromTemplateTest)
		{
			Assert::IsNotNull(_ief);
			Assert::IsNotNull(_ief->GetHeader());
			Assert::IsNotNull(_ief->GetBody());
			Assert::IsNotNull(_ief->GetHeaderProcSteps());
			Assert::IsNotNull(_ief->GetBodyImages());

			Assert::IsNotNull(TinyXmlHelper::FindChildXmlElement(_ief->GetHeader(), "TestMeHeader"));
			Assert::IsNotNull(TinyXmlHelper::FindChildXmlElement(_ief->GetHeader(),  ImageProcSteps::xmlProcSteps));
			Assert::IsNotNull(TinyXmlHelper::FindChildXmlElement(_ief->GetBody(), "TestMeBody"));
			Assert::IsNotNull(TinyXmlHelper::FindChildXmlElement(_ief->GetBody(), ImageProcessFile::xmlImages));
			Assert::AreEqual(ImageProcessFile::xmlImages, _ief->GetBodyImages()->Value());
		}

		TEST_METHOD(IEFFromDefaultsTest)
		{
			Assert::IsNotNull(_iefTemplate);
			Assert::IsNotNull(_iefTemplate->GetHeader());
			Assert::IsNotNull(_iefTemplate->GetBody());
			Assert::IsNotNull(_iefTemplate->GetHeaderProcSteps());
			Assert::IsNotNull(_iefTemplate->GetBodyImages());

			Assert::IsNotNull(TinyXmlHelper::FindChildXmlElement(_iefTemplate->GetHeader(), "TestMeHeader"));
			Assert::IsNotNull(TinyXmlHelper::FindChildXmlElement(_iefTemplate->GetHeader(),  ImageProcSteps::xmlProcSteps));
			Assert::IsNotNull(TinyXmlHelper::FindChildXmlElement(_iefTemplate->GetBody(), "TestMeBody"));
			Assert::IsNotNull(TinyXmlHelper::FindChildXmlElement(_iefTemplate->GetBody(), ImageProcessFile::xmlImages));
			Assert::IsNotNull(_iefTemplate->GetBodyImages());
			Assert::AreEqual(ImageProcessFile::xmlImages,  _iefTemplate->GetBodyImages()->Value());
		}

		TEST_METHOD(SaveApiXmlTest)
		{
			_Lib->ClearAllFunctions();

			DummyOne *proc = new DummyOne();
			Assert::IsTrue(proc->IsApiValid());

			Assert::IsTrue(_Lib->AddFunction(proc));
			Assert::IsTrue(_Lib->Validate());
			Assert::IsTrue( _Lib->IsValid());
			Assert::AreEqual(1, _Lib->GetFunctionCount());

			Assert::IsTrue(_Lib->SaveLibFunctionDefsToXml(TestFiles::UnitTestIEFTemplatewStr, TestFiles::UnitTestPluginDirwStr ));
		}

	};
}